home *** CD-ROM | disk | FTP | other *** search
/ Windows 6-Pak - Disc 5 / Windows 6-Pak (InfoMagic) (Disc 5) (1999).ISO / C&C++Tools / sbparser.exe / cppb / ParseDemo.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-26  |  12.8 KB  |  417 lines

  1. //---------------------------------------------------------------------------
  2. #include <vcl\vcl.h>
  3. #pragma hdrstop
  4.  
  5. #include "ParseDemo.h"
  6. #include "SpeedTest.h"
  7. #include "PlotTest.h"
  8. //---------------------------------------------------------------------------
  9. #pragma resource "*.dfm"
  10. TFParser *FParser;
  11. //---------------------------------------------------------------------------
  12. __fastcall TFParser::TFParser(TComponent* Owner)
  13.     : TForm(Owner)
  14. {
  15.     hParser = NULL;
  16. }
  17. //---------------------------------------------------------------------------
  18. void __fastcall TFParser::FormCreate(TObject *Sender)
  19. {
  20.     //Loading of the csbparse.DLL
  21.     if (InitSTDsbParserDLL() == true) {
  22.  
  23.            //Create an sbParser object and receive its HANDLE
  24.            hParser = CreateNewParser("0", 6, "xyzuvw");
  25.           if (GetIsError(hParser)) {
  26.  
  27.             char errText[50];
  28.  
  29.              //An error has occurred (see documentation)
  30.              MessageBox(Handle, ldtochar(errText, GetGlobalError(hParser)),
  31.                 "Errornumber in Function (see documentation)",
  32.                 MB_ICONERROR);
  33.             Application->Terminate();
  34.         }
  35.     }
  36.     else { //Loading csbparse.DLL failed
  37.         Application->Terminate();
  38.     }
  39. }
  40. //---------------------------------------------------------------------------
  41. void __fastcall TFParser::FormShow(TObject *Sender)
  42. {
  43.     char tempstr[50];
  44.  
  45.     Caption = "Demo for Borland C++ Builder ";
  46. //    Caption = Caption + GetParserVersionNumber(tempstr);
  47.     MFunction->Text = "0";
  48. }
  49.  
  50. void TFParser::GetBaseAndAngUnit(TForm* pForm,
  51.     int* BaseIn, int* BaseOut, int* AngularUnit)
  52. {
  53.        //Get the base for the input
  54.     if (RBDEC->Checked)
  55.         *BaseIn = 10;
  56.     if (RBBIN->Checked)
  57.         *BaseIn = 2;
  58.     if (RBHEX->Checked)
  59.         *BaseIn = 16;
  60.     if (RBOCT->Checked)
  61.         *BaseIn = 8;
  62.  
  63.        //Get the base for the output
  64.     if (RBDECOut->Checked)
  65.         *BaseOut = 10;
  66.     if (RBBINOut->Checked)
  67.         *BaseOut = 2;
  68.     if (RBHEXOut->Checked)
  69.         *BaseOut = 16;
  70.     if (RBOCTOut->Checked)
  71.         *BaseOut = 8;
  72.  
  73.        //Get the angular unit
  74.     if (RBDEG->Checked)
  75.         *AngularUnit = 1;
  76.     if (RBRAD->Checked)
  77.         *AngularUnit = 2;
  78.     if (RBGRA->Checked)
  79.         *AngularUnit = 3;
  80. }
  81.  
  82. bool TFParser::GetVarValues(TForm* pForm, int BaseIn, long double varValues[])
  83. {
  84.     bool err = false;
  85.     int errPos = -1;
  86.  
  87.        //Convert the strings of the variables to long double
  88.     varValues[0]=ConvertBaseTold(ExValue->Text.c_str(), BaseIn, &errPos);
  89.        if (errPos >= 0) {
  90.       MessageBox(pForm->Handle, "The string of the variable x causes an error.", "Error",
  91.          MB_ICONERROR);
  92.       err=true;
  93.        }
  94.     varValues[1]=ConvertBaseTold(EyValue->Text.c_str(), BaseIn, &errPos);
  95.        if (errPos >= 0) {
  96.       MessageBox(pForm->Handle, "The string of the variable y causes an error.", "Error",
  97.          MB_ICONERROR);
  98.       err=true;
  99.        }
  100.     varValues[2]=ConvertBaseTold(EzValue->Text.c_str(), BaseIn, &errPos);
  101.        if (errPos >= 0) {
  102.       MessageBox(pForm->Handle, "The string of the variable z causes an error.", "Error",
  103.          MB_ICONERROR);
  104.       err=true;
  105.        }
  106.     varValues[3]=ConvertBaseTold(EuValue->Text.c_str(), BaseIn, &errPos);
  107.        if (errPos >= 0) {
  108.       MessageBox(pForm->Handle, "The string of the variable u causes an error.", "Error",
  109.          MB_ICONERROR);
  110.       err=true;
  111.        }
  112.     varValues[4]=ConvertBaseTold(EvValue->Text.c_str(), BaseIn, &errPos);
  113.        if (errPos >= 0) {
  114.       MessageBox(pForm->Handle, "The string of the variable v causes an error.", "Error",
  115.          MB_ICONERROR);
  116.       err=true;
  117.        }
  118.     varValues[5]=ConvertBaseTold(EwValue->Text.c_str(), BaseIn, &errPos);
  119.        if (errPos >= 0) {
  120.       MessageBox(pForm->Handle, "The string of the variable w causes an error.", "Error",
  121.          MB_ICONERROR);
  122.       err=true;
  123.        }
  124.  
  125.     return err;
  126. }
  127.  
  128. bool TFParser::Calculate(TForm* pForm, char* Function, long double varValues[],
  129.     int BaseIn, int BaseOut, int AngularUnit, long double* Result)
  130. {
  131.     bool err = false;
  132.  
  133.        if (strlen(Function) > 0)
  134.        {
  135.         char errText[50];
  136.  
  137.         SetBaseTo(hParser, BaseIn);
  138.  
  139.         if (GetIsOriginFunc(hParser, Function) == false) {
  140.             Screen->Cursor = crHourGlass;
  141.             SetNewFunction(hParser, Function);
  142.             Screen->Cursor = crDefault;
  143.  
  144.             if (GetIsError(hParser)) {
  145.                 //An error has occurred (see documentation)
  146.                 MessageBox(pForm->Handle, ldtochar(errText, GetGlobalError(hParser)),
  147.                     "Errornumber in Function (see documentation)",
  148.                     MB_ICONERROR);
  149.  
  150.                 *Result = 0;
  151.                 err = true;
  152.             }
  153.         }
  154.  
  155.           if (err == false) {
  156.             SetAngularUnitTo(hParser, AngularUnit);
  157.  
  158.               //Compute the result
  159.              *Result = GetResultExt(hParser, varValues);
  160.  
  161.              if (GetIsError(hParser)) {
  162.                  //An error has occurred (see documentation)
  163.                 MessageBox(pForm->Handle, ldtochar(errText, GetGlobalError(hParser)),
  164.                        "Errornumber in Calculation (see documentation)", MB_ICONERROR);
  165.  
  166.                 *Result = 0;
  167.                    err = true;
  168.              }
  169.              else {
  170.                 err = false;
  171.              }
  172.           }
  173.        }
  174.        else
  175.        {
  176.       if (pForm != FPlotTest)
  177.       MessageBox(pForm->Handle, "There is no function to compute.", "Error",
  178.          MB_ICONERROR);
  179.        }
  180.  
  181.     return err;
  182. }
  183.  
  184. //---------------------------------------------------------------------------
  185. void __fastcall TFParser::BResultClick(TObject *Sender)
  186. {
  187.     long ctime = GetCurrentTime();
  188.  
  189.     int BaseIn;
  190.     int BaseOut;
  191.     int AngularUnit;
  192.  
  193.     GetBaseAndAngUnit(this, &BaseIn, &BaseOut, &AngularUnit);
  194.  
  195.     long double varValues[6];
  196.     bool err = false;
  197.  
  198.     err = GetVarValues(this, BaseIn, varValues);
  199.  
  200.        if (err==false)
  201.     {
  202.           long double Result;
  203.            char tempstr[50];
  204.  
  205.         Screen->Cursor = crHourGlass;
  206.         err = Calculate(this, MFunction->Text.c_str(), varValues,
  207.             BaseIn, BaseOut, AngularUnit, &Result);
  208.         Screen->Cursor = crDefault;
  209.  
  210.         //Convert the long double to a string
  211.         ConvertToBase(tempstr,Result,15,BaseOut);
  212.  
  213.         EResult->Text = tempstr;
  214.     }
  215.  
  216.     if (err) {
  217.         EResult->Text = "Error";
  218.         ctime = 0;
  219.     }
  220.     else {
  221.         ctime = GetCurrentTime() - ctime;
  222.     }
  223.  
  224.     char tempstr[50];
  225.  
  226.     LtheTime->Caption = ldtochar(tempstr, (long double)ctime/1000, 6);
  227. }
  228. //---------------------------------------------------------------------------
  229. void __fastcall TFParser::MFunctionChange(TObject *Sender)
  230. {
  231.     EResult->Text = "no result ...";
  232. }
  233. //---------------------------------------------------------------------------
  234. void __fastcall TFParser::BExitClick(TObject *Sender)
  235. {
  236.     Close();
  237. }
  238. //---------------------------------------------------------------------------
  239. void __fastcall TFParser::FormDestroy(TObject *Sender)
  240. {
  241.        //Delete of the sbParser object
  242.        DeleteParser(hParser);
  243.  
  244.     //Unloading of the csbparse.DLL
  245.     DeinitSTDsbParserDLL();
  246. }
  247. //---------------------------------------------------------------------------
  248. void __fastcall TFParser::BSaveClick(TObject *Sender)
  249. {
  250.     TFileName NameOfFile;
  251.     AnsiString BaseIn;
  252.     AnsiString BaseOut;
  253.     AnsiString AngularUnit;
  254.     char tempstr[50];
  255.  
  256.     if(DSave->Execute()) {
  257.         MFunctionChange(Sender);
  258.         Caption = "Demo for Borland C++ Builder ";
  259. //        Caption = Caption + GetParserVersionNumber(tempstr);
  260.         Caption = Caption + " - ";
  261.         Caption = Caption + DSave->FileName;
  262.  
  263.         NameOfFile = DSave->FileName;
  264.         if (NameOfFile.Length() > 3) {
  265.             if (NameOfFile[NameOfFile.Length()-3] != '.')
  266.                 NameOfFile = NameOfFile + ".sbc";
  267.         }
  268.         else {
  269.                 NameOfFile = NameOfFile + ".sbc";
  270.         }
  271.         WritePrivateProfileString("Data", "f(x,y,z,u,v,w)",
  272.             MFunction->Text.c_str(), NameOfFile.c_str());
  273.  
  274.         WritePrivateProfileString("Data", "xVariable",
  275.             ExValue->Text.c_str(), NameOfFile.c_str());
  276.         WritePrivateProfileString("Data", "yVariable",
  277.             EyValue->Text.c_str(), NameOfFile.c_str());
  278.         WritePrivateProfileString("Data", "zVariable",
  279.             EzValue->Text.c_str(), NameOfFile.c_str());
  280.         WritePrivateProfileString("Data", "uVariable",
  281.             EuValue->Text.c_str(), NameOfFile.c_str());
  282.         WritePrivateProfileString("Data", "vVariable",
  283.             EvValue->Text.c_str(), NameOfFile.c_str());
  284.         WritePrivateProfileString("Data", "wVariable",
  285.             EwValue->Text.c_str(), NameOfFile.c_str());
  286.  
  287.         if (RBDEC->Checked)
  288.             BaseIn = "10";
  289.         if (RBBIN->Checked)
  290.             BaseIn = "2";
  291.         if (RBHEX->Checked)
  292.             BaseIn = "16";
  293.         if (RBOCT->Checked)
  294.             BaseIn = "8";
  295.         WritePrivateProfileString("Data", "BaseIn",
  296.             BaseIn.c_str(), NameOfFile.c_str());
  297.  
  298.         if (RBDECOut->Checked)
  299.             BaseOut = "10";
  300.         if (RBBINOut->Checked)
  301.             BaseOut = "2";
  302.         if (RBHEXOut->Checked)
  303.             BaseOut = "16";
  304.         if (RBOCTOut->Checked)
  305.             BaseOut = "8";
  306.         WritePrivateProfileString("Data", "BaseOut",
  307.             BaseOut.c_str(), NameOfFile.c_str());
  308.  
  309.         if (RBDEG->Checked)
  310.             AngularUnit = "1";
  311.         if (RBRAD->Checked)
  312.             AngularUnit = "2";
  313.         if (RBGRA->Checked)
  314.             AngularUnit = "3";
  315.         WritePrivateProfileString("Data", "AngularUnit",
  316.             AngularUnit.c_str(), NameOfFile.c_str());
  317.     }
  318. }
  319. //---------------------------------------------------------------------------
  320. void __fastcall TFParser::BOpenClick(TObject *Sender)
  321. {
  322.     TFileName NameOfFile;
  323.     char* tempStr = new char[65000];
  324.     AnsiString BaseIn;
  325.     AnsiString BaseOut;
  326.     AnsiString AngularUnit;
  327.     char tempstr[50];
  328.  
  329.     if(DOpen->Execute()) {
  330.         MFunctionChange(Sender);
  331.         Caption = "Demo for Borland C++ Builder ";
  332. //        Caption = Caption + GetParserVersionNumber(tempstr);
  333.         Caption = Caption + " - ";
  334.         Caption = Caption + DOpen->FileName;
  335.  
  336.         NameOfFile = DOpen->FileName;
  337.         GetPrivateProfileString("Data", "f(x,y,z,u,v,w)", "", tempStr,
  338.             64999, NameOfFile.c_str());
  339.         MFunction->Text = tempStr;
  340.  
  341.         GetPrivateProfileString("Data", "xVariable", "0", tempStr,
  342.             64999, NameOfFile.c_str());
  343.         ExValue->Text = tempStr;
  344.         GetPrivateProfileString("Data", "yVariable", "0", tempStr,
  345.             64999, NameOfFile.c_str());
  346.         EyValue->Text = tempStr;
  347.         GetPrivateProfileString("Data", "zVariable", "0", tempStr,
  348.             64999, NameOfFile.c_str());
  349.         EzValue->Text = tempStr;
  350.         GetPrivateProfileString("Data", "uVariable", "0", tempStr,
  351.             64999, NameOfFile.c_str());
  352.         EuValue->Text = tempStr;
  353.         GetPrivateProfileString("Data", "vVariable", "0", tempStr,
  354.             64999, NameOfFile.c_str());
  355.         EvValue->Text = tempStr;
  356.         GetPrivateProfileString("Data", "wVariable", "0", tempStr,
  357.             64999, NameOfFile.c_str());
  358.         EwValue->Text = tempStr;
  359.  
  360.         GetPrivateProfileString("Data", "BaseIn", "10", tempStr,
  361.             64999, NameOfFile.c_str());
  362.         BaseIn = tempStr;
  363.         if (BaseIn == "10")
  364.             RBDEC->Checked = true;
  365.         if (BaseIn == "2")
  366.             RBBIN->Checked = true;
  367.         if (BaseIn == "16")
  368.             RBHEX->Checked = true;
  369.         if (BaseIn == "8")
  370.             RBOCT->Checked = true;
  371.  
  372.         GetPrivateProfileString("Data", "BaseOut", "10", tempStr,
  373.             64999, NameOfFile.c_str());
  374.         BaseOut = tempStr;
  375.         if (BaseOut == "10")
  376.             RBDECOut->Checked = true;
  377.         if (BaseOut == "2")
  378.             RBBINOut->Checked = true;
  379.         if (BaseOut == "16")
  380.             RBHEXOut->Checked = true;
  381.         if (BaseOut == "8")
  382.             RBOCTOut->Checked = true;
  383.  
  384.         GetPrivateProfileString("Data", "AngularUnit", "2", tempStr,
  385.             64999, NameOfFile.c_str());
  386.         AngularUnit = tempStr;
  387.         if (AngularUnit == "1")
  388.             RBDEG->Checked = true;
  389.         if (AngularUnit == "2")
  390.             RBRAD->Checked = true;
  391.         if (AngularUnit == "3")
  392.             RBGRA->Checked = true;
  393.     }
  394.     delete[] tempStr;
  395. }
  396. //---------------------------------------------------------------------------
  397. void __fastcall TFParser::BHelpClick(TObject *Sender)
  398. {
  399.  char* helpstr = "sbParser";
  400.  
  401.  WinHelp(Handle, "..\\help\\csbparse.hlp", HELP_KEY, (unsigned long)helpstr);
  402. }
  403. //---------------------------------------------------------------------------
  404.  
  405. void __fastcall TFParser::BSpeedTestClick(TObject *Sender)
  406. {
  407.     FSpeedTest->ShowModal();
  408. }
  409. //---------------------------------------------------------------------------
  410.  
  411. void __fastcall TFParser::BPlotTestClick(TObject *Sender)
  412. {
  413.     FPlotTest->ShowModal();
  414. }
  415. //---------------------------------------------------------------------------
  416.  
  417.